//
//  ConfirmProductCell.swift
//  koolcar
//
//  Created by xiantian on 2023/11/2.
//

import UIKit

class ConfirmProductCell: UITableViewCell {
    var imgView: UIImageView = {
        let tempImgView = UIImageView()
        tempImgView.backgroundColor = .lightGray
        tempImgView.contentMode = .scaleAspectFill
        tempImgView.clipsToBounds = true
        return tempImgView
    }()
    
    var titleLab: UILabel = {
        let tempLab = UILabel()
        tempLab.textColor = Theme.titleLabColor
        tempLab.backgroundColor = UIColor.clear
        tempLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 14)
        tempLab.numberOfLines = 0
        tempLab.lineBreakMode = .byTruncatingTail
        tempLab.textAlignment = .left
        return tempLab
    }()
    
    var typeLab: UILabel = {
        let tempLab = UILabel()
        tempLab.textColor = Theme.titleLabColor
        tempLab.backgroundColor = UIColor.clear
        tempLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 14)
        tempLab.numberOfLines = 1
        tempLab.lineBreakMode = .byTruncatingTail
        tempLab.textAlignment = .left
        return tempLab
    }()
    
    var priceLab: UILabel = {
        let tempLab = UILabel()
        tempLab.textColor = Theme.titleLabColor
        tempLab.backgroundColor = UIColor.clear
        tempLab.font = UIFont.init(name: Theme.PingFangSCMedium, size: 14)
        tempLab.numberOfLines = 1
        tempLab.lineBreakMode = .byTruncatingTail
        tempLab.textAlignment = .left
        return tempLab
    }()
    
    var quantityLab: UILabel = {
        let tempLab = UILabel()
        tempLab.textColor = Theme.titleLabColor
        tempLab.backgroundColor = UIColor.clear
        tempLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 12)
        tempLab.numberOfLines = 1
        tempLab.lineBreakMode = .byTruncatingTail
        tempLab.textAlignment = .right
        return tempLab
    }()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        contentView.addSubview(imgView)
        contentView.addSubview(titleLab)
        contentView.addSubview(typeLab)
        contentView.addSubview(priceLab)
        contentView.addSubview(quantityLab)
        self.addSnpLayout()
    }
    
    private func addSnpLayout(){
        imgView.snp.makeConstraints { make in
            make.left.equalTo(14)
            make.top.equalTo(10)
            make.width.equalTo(90)
            make.height.equalTo(68)
            make.bottom.lessThanOrEqualTo(-10).priority(.medium)
        }
        
        titleLab.snp.makeConstraints { make in
            make.left.equalTo(imgView.snp.right).offset(10)
            make.top.equalTo(10)
            make.height.greaterThanOrEqualTo(20)
            make.right.equalTo(-14)
        }
        
        typeLab.snp.makeConstraints { make in
            make.left.equalTo(imgView.snp.right).offset(10)
            make.top.equalTo(titleLab.snp.bottom).offset(6)
            make.height.greaterThanOrEqualTo(20)
            make.right.equalTo(-14)
        }
        
        priceLab.snp.makeConstraints { make in
            make.left.equalTo(imgView.snp.right).offset(10)
            make.top.equalTo(typeLab.snp.bottom).offset(6)
            make.height.greaterThanOrEqualTo(22)
            make.bottom.lessThanOrEqualTo(-10).priority(.high)
        }
        
        quantityLab.snp.makeConstraints { make in
            make.left.equalTo(priceLab.snp.right)
            make.right.equalTo(-14)
            make.height.greaterThanOrEqualTo(20)
            make.centerY.equalTo(priceLab.snp.centerY)
        }
    }
    
    func setConfirmProductCell(data: PreviewProductItemModel) {
        let imagesArray = (data.productImages ?? "").components(separatedBy: ",")
        if imagesArray.count > 0, let url = URL.init(string: imagesArray.first ?? "") {
            self.imgView.sd_setImage(with: url, placeholderImage: UIImage.init(named: "icon_default_place"))
        }else{
            self.imgView.image = UIImage.init(named: "icon_default_place")
        }
        self.titleLab.text = data.name ?? ""
        self.typeLab.text = data.parameterSnapshot ?? ""
        self.priceLab.text = String(format: "%@%.2f", XTManageCenter.currentSign(), data.price ?? 0)
        self.quantityLab.text = String(format: "x%d", data.quantity ?? 1)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
